package com.ge.modules.api.secondController;

import com.ge.common.annotation.LoginStoreApi;
import com.ge.common.utils.DeleteType;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.service.TbCommodityService;
import com.ge.modules.entity.TbCommodityEntity;
import com.ge.modules.entity.TbStoreEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 商品/作品
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-04-11 17:29:31
 */
@RestController
@RequestMapping("/api/store/commodity")
public class TbStoreCommodityController {
    @Autowired
    private TbCommodityService tbCommodityService;

    /**
     * 查询商品/作品列表
     *
     * @param tbStoreEntity
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginStoreApi TbStoreEntity tbStoreEntity, @RequestParam Map<String, Object> params) {
        params.put("storeId", tbStoreEntity.getStoreId());
        //查询列表数据
        Query query = new Query(params);
        List<TbCommodityEntity> tbCommodityEntityList = tbCommodityService.queryList(query);
        int total = tbCommodityService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(tbCommodityEntityList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查询商品详细信息
     *
     * @param commodityId
     * @return
     */
    @GetMapping("/info/{commodityId}")
    public R info(@PathVariable("commodityId") Integer commodityId) {
        //通过对象存储条件
        TbCommodityEntity tbCommodityEntity1 = new TbCommodityEntity();
        tbCommodityEntity1.setCommodityId(commodityId);
        TbCommodityEntity tbCommodityEntity = tbCommodityService.queryObject(tbCommodityEntity1);
        return R.ok().put("data", tbCommodityEntity);
    }

    /**
     * 保存商品/作品
     *
     * @param tbStoreEntity
     * @param tbCommodityEntity
     * @return
     */
    @PostMapping("/save")
    public R save(@LoginStoreApi TbStoreEntity tbStoreEntity, TbCommodityEntity tbCommodityEntity) {
        //等于空就添加
        if(tbCommodityEntity.getCommodityId() == null){
            if(tbCommodityEntity.getImgurl().equals("") || tbCommodityEntity.getImgurl() == null){
                tbCommodityEntity.setImgurl("[\"\"]");
            }
            tbCommodityEntity.setStoreId(tbStoreEntity.getStoreId());
            tbCommodityEntity.setUpdateTime(new Date());
            tbCommodityEntity.setCreateTime(new Date());
            tbCommodityEntity.setDeleteType(DeleteType.NOT_DELETE.getKey());
            //默认已上架
            tbCommodityEntity.setPutAwayStatus(1);
            tbCommodityEntity.setDiscounts(tbCommodityEntity.getDiscounts());
            tbCommodityEntity.setPrice(tbCommodityEntity.getPrice());
            tbCommodityService.save(tbCommodityEntity);
        }else{
            if(tbCommodityEntity.getImgurl().equals("") || tbCommodityEntity.getImgurl() == null){
                tbCommodityEntity.setImgurl("[\"\"]");
            }
            tbCommodityEntity.setUpdateTime(new Date());
            tbCommodityService.update(tbCommodityEntity);
        }
        return R.ok().put("data",tbCommodityEntity);
    }

    /**
     * 修改商品上架状态
     *
     * @param tbStoreEntity
     * @param tbCommodityEntity
     * @return
     */
    @PostMapping("/updatePutAwayStatus")
    public R updatePutAwayStatus(@LoginStoreApi TbStoreEntity tbStoreEntity,TbCommodityEntity tbCommodityEntity){
        tbCommodityEntity.setUpdateTime(new Date());
        tbCommodityService.update(tbCommodityEntity);
        return R.ok();
    }

    /**
     * 逻辑删除作品
     *
     * @param ids
     * @return
     */
    @GetMapping("/updateDelete")
    public R updateDelete(Integer[] ids) {
        tbCommodityService.updateBatch(ids);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public R delete(Integer[] ids) {
        tbCommodityService.deleteBatch(ids);
        return R.ok();
    }

}
